<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","images":"/images","scheme":"Muse","version":"8.3.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}};
  </script>
<meta property="og:type" content="website">
<meta property="og:title" content="Lawted&#39;s Blog">
<meta property="og:url" content="http://example.com/page/2/index.html">
<meta property="og:site_name" content="Lawted&#39;s Blog">
<meta property="og:locale" content="en_US">
<meta property="article:author" content="Lawted Wu">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://example.com/page/2/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : true,
    isPost : false,
    lang   : 'en'
  };
</script>
<title>Lawted's Blog</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Lawted's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-overview-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Lawted Wu</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">67</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/LAWTED" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;LAWTED" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner index posts-expand">

    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%BC%BA%E5%88%B6%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2/" class="post-title-link" itemprop="url">强制类型转换</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-19 15:21:08" itemprop="dateCreated datePublished" datetime="2021-04-19T15:21:08+08:00">2021-04-19</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:33:24" itemprop="dateModified" datetime="2021-05-07T09:33:24+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="值类型转换"><a href="#值类型转换" class="headerlink" title="值类型转换"></a>值类型转换</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">42</span></span><br><span class="line"><span class="keyword">var</span> b = a+<span class="string">&#x27;&#x27;</span>      <span class="comment">//隐式强制类型转换</span></span><br><span class="line"><span class="keyword">var</span> c = <span class="built_in">String</span>(a) <span class="comment">//显示强制类型转换</span></span><br></pre></td></tr></table></figure>


<h2 id="抽象值操作"><a href="#抽象值操作" class="headerlink" title="抽象值操作"></a>抽象值操作</h2><h4 id="toString-NaN"><a href="#toString-NaN" class="headerlink" title="toString(..)"></a>toString(..)</h4><p>负责处理非字符串到字符串的强制类型转换</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">10000000000000000</span>*<span class="number">10000000000000000</span>;</span><br><span class="line"><span class="built_in">console</span>.log(a.toString()) <span class="comment">//1e+32</span></span><br></pre></td></tr></table></figure>


<h4 id="JSON字符串化"><a href="#JSON字符串化" class="headerlink" title="JSON字符串化"></a>JSON字符串化</h4><p><code>JSON.stringify</code>将JSON对象序列化为字符串时也用到了<code>toString(..)</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">JSON</span>.stringify(<span class="literal">true</span>)  <span class="comment">//&quot;true&quot;</span></span><br></pre></td></tr></table></figure>


<h3 id="toNumber"><a href="#toNumber" class="headerlink" title="toNumber(..)"></a>toNumber(..)</h3><p>将非数字值当作数字来使用，<code>true</code>转换为<code>1</code>，<code>false</code>转换为<code>0</code>,<code>undefined</code>转换为<code>NaN</code>,<code>null</code>转换为<code>0</code></p>
<h2 id="显式强制类型转换"><a href="#显式强制类型转换" class="headerlink" title="显式强制类型转换"></a>显式强制类型转换</h2><h3 id="字符串和数字之间的显式转换"><a href="#字符串和数字之间的显式转换" class="headerlink" title="字符串和数字之间的显式转换"></a>字符串和数字之间的显式转换</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">42</span></span><br><span class="line"><span class="keyword">var</span> b = <span class="built_in">String</span>(a)</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = <span class="string">&quot;3.14&quot;</span></span><br><span class="line"><span class="keyword">var</span> d = <span class="built_in">Number</span>(c)</span><br><span class="line"><span class="keyword">var</span> e = +c</span><br></pre></td></tr></table></figure>


<h4 id="日期显式转换为数字"><a href="#日期显式转换为数字" class="headerlink" title="日期显式转换为数字"></a>日期显式转换为数字</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> d = <span class="keyword">new</span> <span class="built_in">Date</span>(<span class="string">&quot;Mon, 19 Apr 2021 16:15:33 CDT&quot;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(+d)</span><br><span class="line"><span class="comment">//下面的方式同样可以获取时间戳</span></span><br><span class="line"><span class="keyword">var</span> timestamp1 = +<span class="keyword">new</span> <span class="built_in">Date</span>()</span><br><span class="line"><span class="keyword">var</span> timestamp2 = <span class="built_in">Date</span>.now()</span><br></pre></td></tr></table></figure>


<h4 id="显式转换为布尔值"><a href="#显式转换为布尔值" class="headerlink" title="显式转换为布尔值"></a>显式转换为布尔值</h4><p>显式强制类型转换为布尔值最常用的方法是<code>!!</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;0&quot;</span></span><br><span class="line"><span class="keyword">var</span> b = []</span><br><span class="line"><span class="keyword">var</span> c = &#123;&#125;</span><br><span class="line"><span class="built_in">console</span>.log(!!a)</span><br><span class="line"><span class="built_in">console</span>.log(!!b)</span><br><span class="line"><span class="built_in">console</span>.log(!!c)</span><br></pre></td></tr></table></figure>


<h3 id="操作符"><a href="#操作符" class="headerlink" title="~操作符"></a><code>~</code>操作符</h3><p>在for循环中，可以使用<code>~</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">for</span>(<span class="keyword">var</span> i = <span class="number">10</span> ;~i;i--)&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(i)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>相比于<code>i&gt;-1</code>或者<code>i&gt;=0</code>来说<code>~i</code>更加优雅，注意<code>~-1==0</code>,<code>~0==-1</code>,<code>~1==-2</code></p>
<h2 id="隐式强制类型转换"><a href="#隐式强制类型转换" class="headerlink" title="隐式强制类型转换"></a>隐式强制类型转换</h2><h4 id="字符串和数字之间的隐式强制类型转换"><a href="#字符串和数字之间的隐式强制类型转换" class="headerlink" title="字符串和数字之间的隐式强制类型转换"></a>字符串和数字之间的隐式强制类型转换</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;42&quot;</span></span><br><span class="line"><span class="keyword">var</span> b = <span class="string">&quot;0&quot;</span></span><br><span class="line"><span class="keyword">var</span> c = <span class="number">42</span></span><br><span class="line"><span class="keyword">var</span> d = <span class="number">0</span></span><br><span class="line"><span class="keyword">var</span> e = [<span class="number">1</span>,<span class="number">2</span>]</span><br><span class="line"><span class="keyword">var</span> f = [<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a+b) <span class="comment">//&#x27;420&#x27;,字符串拼接</span></span><br><span class="line"><span class="built_in">console</span>.log(c+d) <span class="comment">//42，数字加法</span></span><br><span class="line"><span class="built_in">console</span>.log(e+f) <span class="comment">//1,23,4，转换成字符串拼接</span></span><br></pre></td></tr></table></figure>


<h3 id="隐式强制类型转换到布尔值"><a href="#隐式强制类型转换到布尔值" class="headerlink" title="隐式强制类型转换到布尔值"></a>隐式强制类型转换到布尔值</h3><ul>
<li><code>if(..)</code>语句中的条件判断语句</li>
<li><code>for(..;..;..)</code>语句的条件判断表达式</li>
<li><code>while(..)</code>和<code>do..while(..)</code>循环中的条件判断表达式</li>
<li>三元判断</li>
<li>逻辑运算符 <code>||</code>和<code>&amp;&amp;</code>左边的操作数</li>
</ul>
<h3 id="和-amp-amp"><a href="#和-amp-amp" class="headerlink" title="|| 和 &amp;&amp;"></a>|| 和 &amp;&amp;</h3><p>逻辑运算符和C中不一样</p>
<p>首先判断第一个的结果</p>
<p>对于<code>||</code>来说，如果条件判断结果是<code>true</code>会返回第一个操作数的值，如果为<code>false</code>则返回第二个操作数的值</p>
<p>对于<code>&amp;&amp;</code>来说，如果条件判断结果是<code>true</code>会返回第二个操作数的值，如果为<code>false</code>则返回第一个操作数的值</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log( <span class="number">4</span> || <span class="number">0</span>) <span class="comment">//4,4转换为true然后因为是或运算，直接返回第一个值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">0</span> || <span class="number">4</span>) <span class="comment">//4,0转换为false,或运算再看看第二个的值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">4</span> &amp;&amp; <span class="number">0</span>) <span class="comment">//0,4转换为true,与运算再看看第二个的值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">0</span> &amp;&amp; <span class="number">4</span>) <span class="comment">//0,0转换为false,与运算不看第二个直接返回第一个的值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">5</span> &amp;&amp; <span class="number">4</span>) <span class="comment">//4,5转换为true,再看看第二个的值</span></span><br><span class="line"><span class="built_in">console</span>.log( <span class="number">4</span> &amp;&amp; <span class="number">5</span>) <span class="comment">//5,4转换为true,再看看第二个的值</span></span><br></pre></td></tr></table></figure>
<p>类似于</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">a || b</span><br><span class="line">a ? a : b</span><br><span class="line"></span><br><span class="line">a &amp;&amp; b</span><br><span class="line">a ? b : a</span><br></pre></td></tr></table></figure>


<p>一个非常常见的用法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">say</span> (<span class="params">a,b</span>)</span>&#123;</span><br><span class="line">    a = a||<span class="string">&quot;hello&quot;</span></span><br><span class="line">    b = b||<span class="string">&quot;world&quot;</span></span><br><span class="line">    <span class="built_in">console</span>.log(a+ <span class="string">&quot; &quot;</span> +b)</span><br><span class="line">&#125;</span><br><span class="line">say()           <span class="comment">//hello world</span></span><br><span class="line">say(<span class="string">&quot;ok&quot;</span>,<span class="string">&quot;hey&quot;</span>) <span class="comment">//ok hey</span></span><br></pre></td></tr></table></figure>
<p>如果<code>a</code>没有值，那么就默认是<code>hello</code>，因为这个是或运算，第一个转换为<code>false</code>后就看第二个是什么来决定最终的值</p>
<p>一个不是很常见的压缩方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">foo</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&quot;running foo&quot;</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="number">4</span></span><br><span class="line">a &amp;&amp; foo()</span><br><span class="line"></span><br><span class="line"><span class="comment">//一般我们这样写</span></span><br><span class="line"><span class="keyword">if</span>(a)</span><br><span class="line">foo()</span><br></pre></td></tr></table></figure>
<p><code>foo()</code>只有再条件判断<code>a</code>通过的时候才会被调用，如果条件判断未通过，就不会执行<code>foo()</code></p>
<p>注意： <code>&amp;&amp;</code>的优先级比<code>||</code>的高</p>
<h3 id="null和undefined之间的比较"><a href="#null和undefined之间的比较" class="headerlink" title="null和undefined之间的比较"></a>null和undefined之间的比较</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="literal">null</span> == <span class="literal">undefined</span>) <span class="comment">//true </span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="literal">undefined</span> == <span class="literal">null</span>) <span class="comment">//true</span></span><br></pre></td></tr></table></figure>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%8E%9F%E7%94%9F%E5%87%BD%E6%95%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%8E%9F%E7%94%9F%E5%87%BD%E6%95%B0/" class="post-title-link" itemprop="url">原生函数</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-19 13:24:19" itemprop="dateCreated datePublished" datetime="2021-04-19T13:24:19+08:00">2021-04-19</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:33:29" itemprop="dateModified" datetime="2021-05-07T09:33:29+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="原生函数"><a href="#原生函数" class="headerlink" title="原生函数"></a>原生函数</h2><p>JavaScript中的<code>内建函数</code>，也叫<code>原生函数</code>，常见的<code>原生函数</code>有</p>
<ul>
<li>String()</li>
<li>Number()</li>
<li>Boolean()</li>
<li>Array()</li>
<li>Object()</li>
<li>Function()</li>
<li>RegExp()</li>
<li>Date()</li>
<li>Error()</li>
<li>Symbol()</li>
</ul>
<p>原生函数可以被当作构造函数来使用</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">&quot;abc&quot;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(a) 						<span class="comment">//String &#123;&quot;abc&quot;&#125;</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="keyword">typeof</span> a) 				<span class="comment">//Object </span></span><br><span class="line"><span class="built_in">console</span>.log(a <span class="keyword">instanceof</span> <span class="built_in">String</span>) 	<span class="comment">//true</span></span><br></pre></td></tr></table></figure>


<h3 id="内部属性-class"><a href="#内部属性-class" class="headerlink" title="内部属性[[class]]"></a>内部属性[[class]]</h3><p>内部属性一般通过<code>Object.prototype.toString.call(..)</code>来查看</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.toString.call( [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>] )) <span class="comment">//[object Array]</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.toString.call( <span class="regexp">/^a/g</span> ))   <span class="comment">//[object RegExp]</span></span><br></pre></td></tr></table></figure>
<p>基本类型值被各自的封装对象自动包装</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.toString.call( <span class="string">&quot;abc&quot;</span> )) <span class="comment">//[object String]</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.toString.call( <span class="number">42</span> ))    <span class="comment">//[object Number]</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.toString.call( <span class="literal">true</span> ))  <span class="comment">//[object Boolean]</span></span><br></pre></td></tr></table></figure>
<p><code>null</code>和<code>undefined</code>没有原生构造函数,所以内部[[class]]还是<code>Null</code>和<code>Undefined</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.toString.call( <span class="literal">null</span> ))       <span class="comment">//[object Null]</span></span><br><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Object</span>.prototype.toString.call( <span class="literal">undefined</span> ))  <span class="comment">//[object Undefined]</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h3 id="封装对象自动包装"><a href="#封装对象自动包装" class="headerlink" title="封装对象自动包装"></a>封装对象自动包装</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;abc&#x27;</span></span><br><span class="line"><span class="built_in">console</span>.log(a.length)        <span class="comment">//3</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toUpperCase()) <span class="comment">//ABC</span></span><br></pre></td></tr></table></figure>
<p>Js会自动为基本类型值包装一个封装对象</p>
<h3 id="拆封"><a href="#拆封" class="headerlink" title="拆封"></a>拆封</h3><p>如果想要的到封装对象中的基本类型值，可以使用<code>valueOf()</code>函数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">&quot;abc&quot;</span>)</span><br><span class="line"><span class="keyword">var</span> b = <span class="keyword">new</span> <span class="built_in">Number</span>(<span class="number">42</span>)</span><br><span class="line"><span class="keyword">var</span> c = <span class="keyword">new</span> <span class="built_in">Boolean</span>(<span class="literal">true</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(a.valueOf()) <span class="comment">//abc</span></span><br><span class="line"><span class="built_in">console</span>.log(b.valueOf()) <span class="comment">//42</span></span><br><span class="line"><span class="built_in">console</span>.log(c.valueOf()) <span class="comment">//true</span></span><br></pre></td></tr></table></figure>


<h3 id="原生函数作为构造对象"><a href="#原生函数作为构造对象" class="headerlink" title="原生函数作为构造对象"></a>原生函数作为构造对象</h3><h4 id="Array"><a href="#Array" class="headerlink" title="Array(..)"></a>Array(..)</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>)</span><br><span class="line"><span class="keyword">var</span> b = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//[ 1, 2, 3 ]</span></span><br><span class="line"><span class="built_in">console</span>.log(b) <span class="comment">//[ 1, 2, 3 ]</span></span><br></pre></td></tr></table></figure>
<p>需要注意，如果<code>Array</code>构造函数只带一个数字参数的时候，这个参数会作为数组的预设长度<code>(length)</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> <span class="built_in">Array</span>(<span class="number">5</span>)</span><br><span class="line"><span class="keyword">var</span> b = [<span class="literal">undefined</span>,<span class="literal">undefined</span>,<span class="literal">undefined</span>,<span class="literal">undefined</span>,<span class="literal">undefined</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//[ &lt;5 empty items&gt; ]</span></span><br><span class="line"><span class="built_in">console</span>.log(b) <span class="comment">//[undefined,undefined,undefined,undefined,undefined]</span></span><br></pre></td></tr></table></figure>
<p>a和b并不相同，<strong>永远不要使用或者创建空数组</strong></p>
<h4 id="Object-Funtion-RegExp"><a href="#Object-Funtion-RegExp" class="headerlink" title="Object(..)\Funtion(..)\RegExp(..)"></a>Object(..)\Funtion(..)\RegExp(..)</h4><p><strong>尽量不要使用他们</strong></p>
<p>如果使用<code>Object(..)</code>来创建对象，就没法一次设定多个属性，只能一个一个设定</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> c = <span class="keyword">new</span> <span class="built_in">Object</span>()</span><br><span class="line">c.foo = <span class="string">&quot;bar&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(c) <span class="comment">//&#123; foo: &#x27;bar&#x27; &#125;</span></span><br></pre></td></tr></table></figure>
<p>构造函数<code>Function</code>只在极少数情况下很有用，比如动态定义函数参数和函数体的时候</p>
<p>RegExp(..) 在动态定义正则表达式的时候有用</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> name = <span class="string">&#x27;Lawted&#x27;</span></span><br><span class="line"><span class="keyword">var</span> namepattern = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">&quot;\name\&quot;,&quot;</span>g<span class="string">&quot;)</span></span><br><span class="line"><span class="string">var matches = text.match(namepattern)</span></span><br></pre></td></tr></table></figure>


<h3 id="Date"><a href="#Date" class="headerlink" title="Date(..)"></a>Date(..)</h3><p>可以使用Date.now()啦获取当前时间戳</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">console</span>.log(<span class="built_in">Date</span>.now()) <span class="comment">//1618814998027</span></span><br></pre></td></tr></table></figure>


<h2 id="原生原型"><a href="#原生原型" class="headerlink" title="原生原型"></a>原生原型</h2><p>原生构造函数都有自己的<code>.prototype</code>对象</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">//全部不改变原字符串，返回新字符串</span></span><br><span class="line"><span class="built_in">String</span>.prototype.indexof(..)</span><br><span class="line"><span class="built_in">String</span>.prototype.charAt(..)</span><br><span class="line"><span class="built_in">String</span>.prototype.substr(..)/<span class="built_in">String</span>.prototype.slice(..)</span><br><span class="line"><span class="built_in">String</span>.prototype.toUpperCase(..)/<span class="built_in">String</span>.prototype.toLowerCase(..)</span><br><span class="line"><span class="built_in">String</span>.prototype.trim(..) <span class="comment">//去掉字符串前后的空格</span></span><br></pre></td></tr></table></figure>
<p>tips：<code>slice</code>接收的是起始位置和结束位置(不包括结束位置)，而<code>substr</code>接收的则是起始位置和所要返回的字符串长度。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;       hello i am lawted    &#x27;</span></span><br><span class="line"><span class="keyword">var</span> b = a.trim()</span><br><span class="line"><span class="built_in">console</span>.log(b) <span class="comment">//hello i am lawted</span></span><br></pre></td></tr></table></figure>























      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%80%BC/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/19/JS%20U%20DONT%20KNOW%202/%E5%80%BC/" class="post-title-link" itemprop="url">值</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-19 08:40:12" itemprop="dateCreated datePublished" datetime="2021-04-19T08:40:12+08:00">2021-04-19</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:33:35" itemprop="dateModified" datetime="2021-05-07T09:33:35+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><p>数组可以容纳任何类型的值，可以是<code>字符串</code>，<code>数字</code>，<code>对象</code>，甚至<code>其他数组</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="string">&#x27;2&#x27;</span>,[<span class="number">3</span>]]</span><br><span class="line"><span class="built_in">console</span>.log(a.length)</span><br><span class="line"><span class="built_in">console</span>.log(a[<span class="number">0</span>]===<span class="number">1</span>)    <span class="comment">//3</span></span><br><span class="line"><span class="built_in">console</span>.log(a[<span class="number">1</span>]==<span class="number">2</span>)     <span class="comment">//true</span></span><br><span class="line"><span class="built_in">console</span>.log(a[<span class="number">2</span>][<span class="number">0</span>]===<span class="number">3</span>) <span class="comment">//true</span></span><br></pre></td></tr></table></figure>
<p>声明数组后就可以向里面加入值，不需要预先设定大小</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = []</span><br><span class="line">a[<span class="number">0</span>]=<span class="number">1</span></span><br><span class="line">a[<span class="number">1</span>]=<span class="string">&#x27;2&#x27;</span></span><br><span class="line">a[<span class="number">2</span>]=[<span class="number">3</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a.length) <span class="comment">//3</span></span><br><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//[ 1, &#x27;2&#x27;, [ 3 ] ]</span></span><br></pre></td></tr></table></figure>
<p>如果使用<code>delete</code>可以将单元从数组中删除，但是请注意，单元删除后，数组的<code>length</code>不会发生变化,被删除的地方变成了<code>undefined</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>,<span class="string">&#x27;2&#x27;</span>,[<span class="number">3</span>]]</span><br><span class="line"><span class="keyword">delete</span> a[<span class="number">1</span>]</span><br><span class="line"><span class="built_in">console</span>.log(a.length)<span class="comment">//3</span></span><br><span class="line"><span class="built_in">console</span>.log(a)    	 <span class="comment">//[ 1, &lt;1 empty item&gt;, [ 3 ] ]</span></span><br><span class="line"><span class="built_in">console</span>.log(a[<span class="number">1</span>]) 	 <span class="comment">//undefined</span></span><br></pre></td></tr></table></figure>


<h2 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h2><p>字符串的数组很相似，都有<code>length</code>属性以及<code>indexOf(..)</code>和<code>concat</code>方法</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;foo&#x27;</span></span><br><span class="line"><span class="keyword">var</span> b = [<span class="string">&#x27;f&#x27;</span>,<span class="string">&#x27;o&#x27;</span>,<span class="string">&#x27;o&#x27;</span>]</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(a.length)   <span class="comment">//3</span></span><br><span class="line"><span class="built_in">console</span>.log(b.length)   <span class="comment">//3</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(a.indexOf(<span class="string">&#x27;o&#x27;</span>)) <span class="comment">//1</span></span><br><span class="line"><span class="built_in">console</span>.log(b.indexOf(<span class="string">&#x27;o&#x27;</span>)) <span class="comment">//1</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = a.concat(<span class="string">&quot;bar&quot;</span>) </span><br><span class="line"><span class="keyword">var</span> d = b.concat([<span class="string">&quot;b&quot;</span>,<span class="string">&quot;a&quot;</span>,<span class="string">&quot;r&quot;</span>])</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(c) <span class="comment">//foobar</span></span><br><span class="line"><span class="built_in">console</span>.log(d) <span class="comment">//[ &#x27;f&#x27;, &#x27;o&#x27;, &#x27;o&#x27;, &#x27;b&#x27;, &#x27;a&#x27;, &#x27;r&#x27; ]</span></span><br></pre></td></tr></table></figure>
<p>可以用<code>call</code>来让字符串借用<code>数组</code>的某些函数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;foo&#x27;</span></span><br><span class="line"><span class="keyword">var</span> c = <span class="built_in">Array</span>.prototype.join.call(a,<span class="string">&#x27;-&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(c) <span class="comment">//f-o-o</span></span><br></pre></td></tr></table></figure>
<p>但是字符串不能借用数组的可变更函数，因为字符串是不可变的，比如反转<code>reverse</code>函数，但是可以先把字符串转换成数组，然后再转回去</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&#x27;foo&#x27;</span></span><br><span class="line">a = a.split(<span class="string">&#x27;&#x27;</span>).reverse().join(<span class="string">&#x27;&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//oof</span></span><br></pre></td></tr></table></figure>


<h2 id="数字"><a href="#数字" class="headerlink" title="数字"></a>数字</h2><p>特别答案的数和特别小的数用指数格式显示</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">5E10</span></span><br><span class="line"><span class="built_in">console</span>.log(a) <span class="comment">//50000000000</span></span><br><span class="line"><span class="keyword">var</span> b = <span class="number">5E-3</span></span><br><span class="line"><span class="built_in">console</span>.log(b) <span class="comment">//0.005</span></span><br></pre></td></tr></table></figure>
<p>可以用<code>toFixed(..)</code>方法指定小数部分的显示位数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">42.59</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toFixed(<span class="number">0</span>)) <span class="comment">//43</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toFixed(<span class="number">1</span>)) <span class="comment">//42.6</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toFixed(<span class="number">2</span>)) <span class="comment">//42.59</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toFixed(<span class="number">3</span>)) <span class="comment">//42.590</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toFixed(<span class="number">4</span>)) <span class="comment">//42.5900</span></span><br></pre></td></tr></table></figure>
<p>可以用<code>toPrecision(..)</code>方法指定有效位数的显示位数</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="number">42.59</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toPrecision(<span class="number">1</span>)) <span class="comment">//4e+1</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toPrecision(<span class="number">2</span>)) <span class="comment">//43</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toPrecision(<span class="number">3</span>)) <span class="comment">//42.6</span></span><br><span class="line"><span class="built_in">console</span>.log(a.toPrecision(<span class="number">4</span>)) <span class="comment">//42.59</span></span><br></pre></td></tr></table></figure>
<p> 注意不可以用<code>42.toFixed(3)</code>,因为我们知道<code>42.</code>是可以的，所以它会被识别成<code>42.</code>然后就没有调用<code>toFixed</code>方法</p>
<h3 id="检测"><a href="#检测" class="headerlink" title="检测"></a>检测</h3><p><code>Number.isInteger(..)</code>可以检测一个值是否是整数</p>
<p><code>Number.isSageInteger(..)</code>可以检测一个值是否是安全的整数</p>
<p><code>Number.isNaN</code>可以检测一个参数是不是<code>NaN</code></p>
<h2 id="传递"><a href="#传递" class="headerlink" title="传递"></a>传递</h2><p>简单标量(<code>字符串</code>,<code>数字</code>)  基本类型值通过值复制来赋值/传递，而复合值(<code>对象</code>) 通过引用复制来赋值/传递</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/16/ES6-%E7%9A%84-cls/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/16/ES6-%E7%9A%84-cls/" class="post-title-link" itemprop="url">ES6 的 cls</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-16 15:55:04" itemprop="dateCreated datePublished" datetime="2021-04-16T15:55:04+08:00">2021-04-16</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/15/JS%20U%20DONT%20KNOW%201/%E5%8E%9F%E5%9E%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/15/JS%20U%20DONT%20KNOW%201/%E5%8E%9F%E5%9E%8B/" class="post-title-link" itemprop="url">原型</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-15 18:45:58" itemprop="dateCreated datePublished" datetime="2021-04-15T18:45:58+08:00">2021-04-15</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:32:49" itemprop="dateModified" datetime="2021-05-07T09:32:49+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="Prototype"><a href="#Prototype" class="headerlink" title="[[Prototype]]"></a>[[Prototype]]</h2><p><code>[[Prototype]] </code>也就是浏览器中的<code>__proto__</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> myObject = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(myObject.a);</span><br></pre></td></tr></table></figure>
<p>当引用<code>myObject.a</code>的时候，第一步就是检查对象本身是否有这个属性，如果有的话就使用它，但是如果<code>a</code>不在<code>myObject</code>中的画，就需要使用对象的<code>[[Prototype]]</code>链</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> anotherObject = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;;</span><br><span class="line"><span class="keyword">var</span> myObject = <span class="built_in">Object</span>.create(anotherObject);</span><br><span class="line"><span class="built_in">console</span>.log(myObject.a); <span class="comment">//2</span></span><br></pre></td></tr></table></figure>


<h2 id="Object-prototype"><a href="#Object-prototype" class="headerlink" title="Object.prototype"></a>Object.prototype</h2><p>所有普通的<code>[[Prototype]]</code>链最终都会指向内置的<code>Object.prototype</code>,它包含了JS中许多通用的功能，比如<code>.toString()</code>和<code>valueOf()</code>,还有<code>.hasOwnProperty(..)</code></p>
<h2 id="属性设置和屏蔽"><a href="#属性设置和屏蔽" class="headerlink" title="属性设置和屏蔽"></a>属性设置和屏蔽</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> anotherObject = &#123;</span><br><span class="line">	a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> myObject = <span class="built_in">Object</span>.create(anotherObject);</span><br><span class="line"></span><br><span class="line">myObject.a = <span class="number">3</span>;</span><br><span class="line"><span class="built_in">console</span>.log(myObject.a) <span class="comment">//3</span></span><br><span class="line"><span class="built_in">console</span>.log(anotherObject.a) <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p>如果<code>a</code>不在<code>myObject</code>中而是再原型链上层的时候，而且它是一个普通的数据访问属性，那么就会在<code>myObject</code>中添加一个名为foo的新属性，它是屏蔽属性</p>
<p>但是如果<code>myObject</code>是制度的，那么会抛出错误，如果链上的<code>foo</code>是一个<code>[[setter]]</code>，那么就会调用这个<code>[[setter]]</code></p>
<h2 id="原型继承"><a href="#原型继承" class="headerlink" title="原型继承"></a>原型继承</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Foo</span>(<span class="params">name</span>)</span>&#123;</span><br><span class="line">    <span class="built_in">this</span>.name = name;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Foo.prototype.myName = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">	<span class="keyword">return</span> <span class="built_in">this</span>.name</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">Bar</span>(<span class="params">name,label</span>)</span>&#123;</span><br><span class="line">	Foo.call(<span class="built_in">this</span>,name)</span><br><span class="line">    <span class="built_in">this</span>.label = label</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Bar.prototype = <span class="built_in">Object</span>.create(Foo.prototype)</span><br><span class="line">Bar.prototype.myLabel = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.label</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> a = <span class="keyword">new</span> Bar(<span class="string">&quot;a&quot;</span>,<span class="string">&quot;obj a&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(a.myName())</span><br><span class="line"><span class="built_in">console</span>.log(a.myLabel())</span><br></pre></td></tr></table></figure>
<p><code>Bar.prototype = Object.create(Foo.prototype)</code>创建了新的对象，并且把新对象内部的<code>[[prototype]]</code>关联到了<code>Foo.prototype</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">Bar.prototype = Foo.prototype</span><br><span class="line">Bar.prototype = <span class="keyword">new</span> Foo()</span><br></pre></td></tr></table></figure>
<p>以上两个都是错误的关联对象的方式</p>
<p><code>Bar.prototype = Foo.prototype</code>不会创建一个关联到<code>Bar.prototype</code>的新对象，而是让<code>Bar.prototype</code>直接引用<code>Foo.prototype</code>对象</p>
<p><code>Bar.prototype = new Foo()</code>会创一个关联到<code>Bar.prototype</code>的新对象，但是它使用了<code>Foo</code>的”构造函数调用”,会有佛作用，可能会印象到<code>Bar()</code>的后代</p>
<p>还可以使用</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">Object</span>.setPrototypeOf(Bar.prototype,Foo.prototype) <span class="comment">//ES6</span></span><br></pre></td></tr></table></figure>


<h3 id="判断是否再-Prototype-链中"><a href="#判断是否再-Prototype-链中" class="headerlink" title="判断是否再[[Prototype]]链中"></a>判断是否再<code>[[Prototype]]</code>链中</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line">b.isprototypeof(c)</span><br></pre></td></tr></table></figure>
      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/15/JS%20U%20DONT%20KNOW%201/%E6%B7%B7%E5%90%88%E5%AF%B9%E8%B1%A1%E7%B1%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/15/JS%20U%20DONT%20KNOW%201/%E6%B7%B7%E5%90%88%E5%AF%B9%E8%B1%A1%E7%B1%BB/" class="post-title-link" itemprop="url">混合对象类</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-15 18:37:16" itemprop="dateCreated datePublished" datetime="2021-04-15T18:37:16+08:00">2021-04-15</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:31:59" itemprop="dateModified" datetime="2021-05-07T09:31:59+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="类"><a href="#类" class="headerlink" title="类"></a>类</h2><p>类是一种设计模式，JS也有类似这样的语法，但是和其他语言的类完全不同，传统的类被实例化时候，他的行为会被复制到实例中。类被继承的时候，行为也会被复制到子类中，JS并不会这样自动穿几案对象的副本，所以JS中不常用类</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/12/Get%E5%92%8CSet/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/12/Get%E5%92%8CSet/" class="post-title-link" itemprop="url">Get和Set</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-12 21:13:42" itemprop="dateCreated datePublished" datetime="2021-04-12T21:13:42+08:00">2021-04-12</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-04-16 15:54:27" itemprop="dateModified" datetime="2021-04-16T15:54:27+08:00">2021-04-16</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="Getter-amp-amp-Setter"><a href="#Getter-amp-amp-Setter" class="headerlink" title="Getter &amp;&amp; Setter"></a>Getter &amp;&amp; Setter</h2><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> myObj = &#123;</span><br><span class="line">    <span class="keyword">get</span> <span class="title">a</span>()&#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">myObj.a = <span class="number">3</span></span><br><span class="line"><span class="built_in">console</span>.log(myObj.a) <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p>因为只定义了<code>a</code>的<code>getter</code>，所以对<code>a</code>的值进行设置<code>set</code>的时候会忽略赋值操作，不会抛出错误。</p>
<p>所以给它再定义<code>setter</code></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> myObj = &#123;</span><br><span class="line">    <span class="keyword">get</span> <span class="title">a</span>()&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">this</span>.b</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="keyword">set</span> <span class="title">a</span>(<span class="params">val</span>)&#123;</span><br><span class="line">		<span class="built_in">this</span>.b = val * <span class="number">2</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">myObj.a = <span class="number">2</span>;</span><br><span class="line"><span class="built_in">console</span>.log(myObj.a)</span><br><span class="line"><span class="built_in">console</span>.log(myObj)</span><br></pre></td></tr></table></figure>
<p><code>a</code>是一个<code>get</code>，而他输出的是<code>myObj.b</code>,也就是说<code>myObj.a</code>的赋值就是<code>set a(val)</code>而引用<code>myobj.a</code>就是<code>get a()</code>,如果我在<code>get a()</code>中同时也修改了<code>a</code>那么就会死循环</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/09/JS%20U%20DONT%20KNOW%201/%E5%B1%9E%E6%80%A7%E6%8F%8F%E8%BF%B0%E7%AC%A6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/09/JS%20U%20DONT%20KNOW%201/%E5%B1%9E%E6%80%A7%E6%8F%8F%E8%BF%B0%E7%AC%A6/" class="post-title-link" itemprop="url">属性描述符</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-09 20:18:58" itemprop="dateCreated datePublished" datetime="2021-04-09T20:18:58+08:00">2021-04-09</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:32:23" itemprop="dateModified" datetime="2021-05-07T09:32:23+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="属性描述符"><a href="#属性描述符" class="headerlink" title="属性描述符"></a>属性描述符</h2><p><code>Object.getOwnPropertyDescriptor</code>是属性描述符</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> myObject = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">Object</span>.getOwnPropertyDescriptor(myObject,<span class="string">&quot;a&quot;</span>)</span><br><span class="line"><span class="comment">//&#123;value: 2, writable: true, enumerable: true, configurable: true&#125;</span></span><br></pre></td></tr></table></figure>
<p>会返回<code>writable</code>,<code>enumerable</code>,<code>configurable</code>三个属性</p>
<h3 id="writable"><a href="#writable" class="headerlink" title="writable"></a>writable</h3><p><code>writable</code>决定是否可以修改属性的值</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;&#125;</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(obj,<span class="string">&quot;a&quot;</span>,&#123;</span><br><span class="line">    value:<span class="number">2</span>,</span><br><span class="line">    writable:<span class="literal">false</span>,</span><br><span class="line">    enumerable:<span class="literal">true</span>,</span><br><span class="line">    configurable:<span class="literal">true</span></span><br><span class="line">&#125;)</span><br><span class="line">obj.a = <span class="number">5</span></span><br><span class="line"><span class="built_in">console</span>.log(obj.a) <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p>修改属性值失败，如果在严格模式下，会出错<code>typeError</code>无法修改一个不可改的属性</p>
<h3 id="configurable"><a href="#configurable" class="headerlink" title="configurable"></a>configurable</h3><p>只要属性可以配置的，就可以用<code>defineProperty(..)</code>来修改属性描述符</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> myObj = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line">myObj.a = <span class="number">3</span>;</span><br><span class="line"><span class="built_in">console</span>.log(myObj.a)</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(myObj,<span class="string">&quot;a&quot;</span>,&#123;</span><br><span class="line">    value:<span class="number">4</span>,</span><br><span class="line">    writable:<span class="literal">true</span>,</span><br><span class="line">    configurable:<span class="literal">false</span>,</span><br><span class="line">    enumerable:<span class="literal">true</span></span><br><span class="line">&#125;)</span><br><span class="line"><span class="built_in">Object</span>.defineProperty(myObj,<span class="string">&quot;a&quot;</span>,&#123;</span><br><span class="line">    value:<span class="number">4</span>,</span><br><span class="line">    writable:<span class="literal">true</span>,</span><br><span class="line">    configurable:<span class="literal">true</span>,</span><br><span class="line">    enumerable:<span class="literal">true</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>第二次配置<code>configurable</code>会出现<code>TypeError</code>，因为第一次把<code>configurable</code>修改为<code>false</code>是单项的操作，无法撤销，而且无法删除这个属性</p>
<h3 id="enumerable"><a href="#enumerable" class="headerlink" title="enumerable"></a>enumerable</h3><p>表示是否可枚举,如果把<code>enumerable</code>设置成<code>false</code>，这个属性就不出现在<code>for..in</code>的枚举中，但是仍然可以正常访问它</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> myObj = &#123;</span><br><span class="line">    a:<span class="number">2</span>,</span><br><span class="line">    b:<span class="number">3</span>,</span><br><span class="line">    c:<span class="number">4</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(myObj)</span><br><span class="line"></span><br><span class="line"><span class="built_in">Object</span>.defineProperty(myObj,<span class="string">&quot;a&quot;</span>,&#123;</span><br><span class="line">    value:<span class="number">4</span>,</span><br><span class="line">    writable:<span class="literal">true</span>,</span><br><span class="line">    configurable:<span class="literal">true</span>,</span><br><span class="line">    enumerable:<span class="literal">false</span></span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span>(<span class="keyword">let</span> i <span class="keyword">in</span> myObj)&#123; <span class="comment">//for..of不能遍历对象的属性</span></span><br><span class="line">	<span class="built_in">console</span>.log(myObj[i])</span><br><span class="line">&#125;<span class="comment">//3,4</span></span><br></pre></td></tr></table></figure>
<p>配置了属性<code>a</code>为不可遍历，所以只有3，4</p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/09/JS%20U%20DONT%20KNOW%201/%E5%AF%B9%E8%B1%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/09/JS%20U%20DONT%20KNOW%201/%E5%AF%B9%E8%B1%A1/" class="post-title-link" itemprop="url">对象</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-09 18:38:21" itemprop="dateCreated datePublished" datetime="2021-04-09T18:38:21+08:00">2021-04-09</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:31:53" itemprop="dateModified" datetime="2021-05-07T09:31:53+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h1 id="对象"><a href="#对象" class="headerlink" title="对象"></a>对象</h1><h2 id="1-语法"><a href="#1-语法" class="headerlink" title="1. 语法"></a>1. 语法</h2><p>对象可以通过<code>文字形式</code>和<code>构造形式</code>进行定义</p>
<h4 id="文字语法"><a href="#文字语法" class="headerlink" title="文字语法"></a>文字语法</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> myObj = &#123;</span><br><span class="line">    key:value</span><br><span class="line">    <span class="comment">//...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h4 id="构造语法"><a href="#构造语法" class="headerlink" title="构造语法"></a>构造语法</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> myObj = <span class="keyword">new</span> <span class="built_in">Object</span>();</span><br><span class="line">myObj.key = value;</span><br></pre></td></tr></table></figure>


<p>在文字中可以添加多个键值对，构造中只能一个个添加</p>
<h3 id="类型"><a href="#类型" class="headerlink" title="类型"></a>类型</h3><p>简单基本类型：<code>string</code>,<code>number</code>,<code>boolean</code>,<code>null</code>,<code>umdefined</code></p>
<h3 id="内置对象"><a href="#内置对象" class="headerlink" title="内置对象"></a>内置对象</h3><ul>
<li>String</li>
<li>Number</li>
<li>Boolean</li>
<li>Object</li>
<li>Function</li>
<li>Array</li>
<li>Date</li>
<li>RegExp</li>
<li>Error</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> str1 = <span class="string">&quot;this is a string&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(str1 <span class="keyword">instanceof</span> <span class="built_in">String</span>) <span class="comment">//false</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> str2 = <span class="keyword">new</span> <span class="built_in">String</span>(<span class="string">&quot;I&#x27;m the true string&quot;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(str2 <span class="keyword">instanceof</span> <span class="built_in">String</span>) <span class="comment">//true</span></span><br><span class="line"></span><br><span class="line"><span class="built_in">console</span>.log(str1.length)</span><br></pre></td></tr></table></figure>
<p><code>str1</code>是一个字面量不是一个对象，而<code>str2</code>是一个对象，引擎会自动把自变量转换成<code>String</code>对象,所以可以获取长度</p>
<p>这些内置对象只是一些内置函数，可以当作构造函数去调用,<code>null</code>和<code>undefined</code>都没有对应的构造形式，只有文字形式，而<code>Date</code>只有构造没有文字形式</p>
<h2 id="2-内容"><a href="#2-内容" class="headerlink" title="2. 内容"></a>2. 内容</h2><h4 id="属性访问和键访问"><a href="#属性访问和键访问" class="headerlink" title="属性访问和键访问"></a>属性访问和键访问</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    a:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(obj.a) <span class="comment">//2</span></span><br><span class="line"><span class="built_in">console</span>.log(obj[<span class="string">&quot;a&quot;</span>]) <span class="comment">//2</span></span><br></pre></td></tr></table></figure>
<p> 第一种方法就是属性访问，第二种方法是键访问,键访问可以使用变量</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">&quot;hello&quot;</span></span><br><span class="line"><span class="keyword">var</span> obj = &#123;</span><br><span class="line">    hello:<span class="number">2</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">console</span>.log(obj[a])</span><br></pre></td></tr></table></figure>


<h2 id="3-数组"><a href="#3-数组" class="headerlink" title="3. 数组"></a>3. 数组</h2><p>数组也是对象，虽然下标都是整数，但仍然可以给数组添加属性</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line">arr.a = <span class="string">&quot;hello&quot;</span></span><br><span class="line"><span class="built_in">console</span>.log(arr.length); <span class="comment">//3</span></span><br><span class="line"><span class="built_in">console</span>.log(arr); <span class="comment">//[1,2,3,a:&quot;hello&quot;]</span></span><br><span class="line">arr[<span class="string">&quot;3&quot;</span>] = <span class="number">4</span></span><br><span class="line"><span class="built_in">console</span>.log(arr.length); <span class="comment">//4</span></span><br><span class="line"><span class="built_in">console</span>.log(arr); <span class="comment">//[1,2,3,4,a:&quot;hello&quot;]</span></span><br></pre></td></tr></table></figure>
<p>由上面的例子可以发现，如果添加一个属性不是数字，他的长度<code>length</code>不会发生改变，但是如果添加的键是像一个数字，那么他就会变成一个数字，添加进入数组，然后改变长度<code>length</code></p>

      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




    


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="">
    <link itemprop="mainEntityOfPage" href="http://example.com/2021/04/09/JS%20U%20DONT%20KNOW%201/%E4%BF%AE%E6%94%B9%E5%8E%9F%E6%9C%89%E6%95%B0%E7%BB%84%E7%9A%84%E5%88%86%E7%B1%BB/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="Lawted Wu">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Lawted's Blog">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          <a href="/2021/04/09/JS%20U%20DONT%20KNOW%201/%E4%BF%AE%E6%94%B9%E5%8E%9F%E6%9C%89%E6%95%B0%E7%BB%84%E7%9A%84%E5%88%86%E7%B1%BB/" class="post-title-link" itemprop="url">修改原有数组的分类</a>
        </h2>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2021-04-09 14:26:59" itemprop="dateCreated datePublished" datetime="2021-04-09T14:26:59+08:00">2021-04-09</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">Edited on</span>
        <time title="Modified: 2021-05-07 09:32:35" itemprop="dateModified" datetime="2021-05-07T09:32:35+08:00">2021-05-07</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
          <h2 id="在原有数组上进行修改"><a href="#在原有数组上进行修改" class="headerlink" title="在原有数组上进行修改"></a>在原有数组上进行修改</h2><h4 id="shift"><a href="#shift" class="headerlink" title="shift()"></a>shift()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">arr.shift()</span><br><span class="line"><span class="built_in">console</span>.log(arr) <span class="comment">//[2,3,4]</span></span><br></pre></td></tr></table></figure>


<h4 id="unshift"><a href="#unshift" class="headerlink" title="unshift()"></a>unshift()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">arr.unshift(<span class="number">0</span>)</span><br><span class="line"><span class="built_in">console</span>.log(arr) <span class="comment">//[0,1,2,3,4]</span></span><br></pre></td></tr></table></figure>


<h4 id="pop"><a href="#pop" class="headerlink" title="pop()"></a>pop()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">arr.pop()</span><br><span class="line"><span class="built_in">console</span>.log(arr) <span class="comment">//[1,2,3]</span></span><br></pre></td></tr></table></figure>


<h4 id="reverse"><a href="#reverse" class="headerlink" title="reverse()"></a>reverse()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line">arr.reverse()</span><br><span class="line"><span class="built_in">console</span>.log(arr) <span class="comment">//[4,3,2,1]</span></span><br></pre></td></tr></table></figure>


<h4 id="sort"><a href="#sort" class="headerlink" title="sort()"></a>sort()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">4</span>]</span><br><span class="line">arr.sort()</span><br><span class="line"><span class="built_in">console</span>.log(arr) <span class="comment">//[1,2,3,4]</span></span><br></pre></td></tr></table></figure>


<h3 id="splice"><a href="#splice" class="headerlink" title="splice()"></a>splice()</h3><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr = [<span class="number">1</span>,<span class="number">3</span>,<span class="number">2</span>,<span class="number">4</span>]</span><br><span class="line">arr.splice(<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line"><span class="built_in">console</span>.log(arr) <span class="comment">//[1,4]</span></span><br></pre></td></tr></table></figure>


<h2 id="不修改原数组-需要再赋值"><a href="#不修改原数组-需要再赋值" class="headerlink" title="不修改原数组,需要再赋值"></a>不修改原数组,需要再赋值</h2><h4 id="concat"><a href="#concat" class="headerlink" title="concat()"></a>concat()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr1 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>]</span><br><span class="line"><span class="keyword">var</span> arr2 = [<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>]</span><br><span class="line"><span class="keyword">var</span> arr3 = arr1.concat(arr2)</span><br><span class="line"><span class="built_in">console</span>.log(arr1) <span class="comment">//[1,2,3]</span></span><br><span class="line"><span class="built_in">console</span>.log(arr3) <span class="comment">//[1,2,3,4,5,6]</span></span><br></pre></td></tr></table></figure>


<h4 id="join"><a href="#join" class="headerlink" title="join()"></a>join()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr1 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="keyword">var</span> arr2 = arr1.join(<span class="string">&#x27;!&#x27;</span>)</span><br><span class="line"><span class="built_in">console</span>.log(arr1) <span class="comment">//[1,2,3,4]</span></span><br><span class="line"><span class="built_in">console</span>.log(arr2) <span class="comment">//1!2!3!4</span></span><br></pre></td></tr></table></figure>


<h4 id="slice"><a href="#slice" class="headerlink" title="slice()"></a>slice()</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> arr1 = [<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>]</span><br><span class="line"><span class="keyword">var</span> arr2 = arr1.slice(<span class="number">1</span>,<span class="number">2</span>)</span><br><span class="line"><span class="built_in">console</span>.log(arr1) <span class="comment">//[1,2,3,4]</span></span><br><span class="line"><span class="built_in">console</span>.log(arr2) <span class="comment">//[2]</span></span><br></pre></td></tr></table></figure>



      
    </div>

    
    
    

    <footer class="post-footer">
        <div class="post-eof"></div>
      
    </footer>
  </article>
</div>




  <nav class="pagination">
    <a class="extend prev" rel="prev" href="/"><i class="fa fa-angle-left" aria-label="Previous page"></i></a><a class="page-number" href="/">1</a><span class="page-number current">2</span><a class="page-number" href="/page/3/">3</a><span class="space">&hellip;</span><a class="page-number" href="/page/7/">7</a><a class="extend next" rel="next" href="/page/3/"><i class="fa fa-angle-right" aria-label="Next page"></i></a>
  </nav>


<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Lawted Wu</span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="Total Visitors">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="Total Views">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  


  <script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>


<script>
var options = {
  bottom: '64px', // default: '32px'
  right: '32px', // default: '32px'
  left: 'unset', // default: 'unset'
  time: '0.5s', // default: '0.3s'
  mixColor: '#fff', // default: '#fff'
  backgroundColor: '#fff',  // default: '#fff'
  buttonColorDark: '#000',  // default: '#100f2c'
  buttonColorLight: '#fff', // default: '#fff'
  saveInCookies: true, // default: true,
  label: ' ', // default: ''
  autoMatchOsTheme: true // default: true
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
</script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  






  
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>





</body>
</html>
